﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using com.fukefu.source;

namespace com.fukefu.spider
{
    public class BaiduSpider : Spider
    {
        Source source = new Source();
        BaiduIp baiduIp = new BaiduIp();

        //获取收录
        public int GetShoulu(string keyword)
        {
            return GetShoulu(keyword, 0);
        }
        public int GetShoulu(string keyword, int days)
        {
            string result = Regex.Match(GetHtml(keyword, 1, days), "找到相关结果约?([0-9,]+)个").Groups[1].Value;
            if (!string.IsNullOrEmpty(result))
            {
                return int.Parse(result.Replace(",", ""));
            }
            return 0;
        }
        //抓取网址代码
        public string GetHtml(string url)
        {
            return GetHtml(url, 0, 0);
        }
        public string GetHtml(string url, int resultLength)
        {
            return GetHtml(url, resultLength, 0);
        }
        //获取制定数量网页
        public string GetHtml(string word, int returnNum, int returnDays)
        {
            string html = "";
            int id = 0;
            do
            {
                html = source.Get("http://" + baiduIp.GetIp() + "/s?rn=" + returnNum + "&lm=" + returnDays + "&wd=" + MyFunction.EncodeByGb2312(word));
                if (html.Equals("") || html.IndexOf("百度--您的访问出错了") != -1 || html.IndexOf("很抱歉，您的电脑或所在的局域网络有异常的访问，此刻我们无法响应您的请求。") != -1)
                {
                    baiduIp.Change();
                    Thread.Sleep(20000 * id);
                }
                else
                {
                    break;
                }
            } while (id++ < 3);
            return html;
        }

        public bool CheckShoulu(string url)
        {
            url = url.Replace("http://", "");
            string html = "";
            //判断inurl中是否有逗号
            if (url.IndexOf(",") == -1)
            {
                html = GetHtml(url);
            }
            else
            {
                html = GetHtml("inurl:(" + url + ")");
                //inurl在百度搜索中的时间间隔是10s，小于10s会提示局域网络有异常的访问
                Thread.Sleep(11000);
            }
            url = Regex.Replace(url, "\\?|\\.|\\+|\\*", "\\$0");
            if (Regex.IsMatch(html, "<a\\s+onmousedown=\"[^\"]+\"\\s+href=\"http://" + url))
            {
                return true;
            }
            return false;
        }
        //获取相关关键字
        public List<string> GetXgKeyword(string keyword)
        {
            string html = GetHtml(keyword, 1);
            List<string> list = new List<string>();
            MatchCollection mc = Regex.Matches(html, "<th><a[^>]+>(.+?)</a>");
            foreach (Match m in mc)
            {
                list.Add(m.Groups[1].Value);
            }
            return list;
        }
    }
}
